home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS04.ADF
/
C
/
timer.c
< prev
next >
Wrap
C/C++ Source or Header
|
1985-12-04
|
4KB
|
138 lines
/* ******************************************************************** */
/* Exec Support Functions: CreateTimer, WaitTimer, DeleteTimer */
/*
/* CreateTimer() returns a pointer to an IOStdReq structure, set up
* for communications with the timer device. Returns NULL if it
* cannot allocate the timer or get enough memory or signals.
*
* WaitTimer(IOStdReq,seconds,microseconds) uses the communications
* block from CreateTimer(). It puts your task to sleep until the
* timer counts down at least this amount of time.
*
* SetTimer(IOStdReq,seconds,microseconds) returns a pointer to a port
* (copied from the IOStdReq) to which the timer returns the message
* block when the timeout has completed. It uses SendIO to transmit
* the IORequest. This means your task can go on to something else,
* then execute WaitPort(timerReplyPort), going to sleep until the
* timer has timed out. You will still have to do a GetMsg(timerReplyPort)
* after your task awakens, where with WaitTimer this is not necessary.
*
* DeleteTimer(IOStdReq) uses the block from CreateTimer() to free up
* memory and signal bits that have been allocated for this purpose.
*
* syntax Summary: struct IOStdReq *CreateTimer();
* int WaitTimer();
* struct Port *SetTimer();
* int DeleteTimer();
*/
#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/ports.h>
#include <exec/io.h>
#include <exec/devices.h>
#include <devices/timer.h>
#define SECONDS io_Actual
#define MICROSECONDS io_Length
/* redefine fields in IOStdReq so as to match requirements of a timeval */
extern struct Port *CreatePort();
extern struct IOStdReq *CreateStdIO();
struct IOStdReq
*CreateTimer() /* return a pointer to an IOStdReq if
* it was possible to allocate a new
* timer */
{
SHORT error;
struct Port *timerport;
struct IOStdReq *timermsg;
timerport = CreatePort(0,0);
if (timerport == NULL)
return(NULL); /* Error during CreatePort */
timermsg = CreateStdIO(timerport);
if (timermsg == NULL)
{
DeletePort(timerport);
return(NULL); /* Error during CreateStdIO */
}
error = OpenDevice(TIMERNAME, UNIT_MICROHZ, timermsg, 0);
if (error != 0)
{
DeleteStdIO(timermsg);
DeletePort(timerport);
return(NULL); /* Error during OpenDevice */
}
return(timermsg);
}
struct Port
*SetTimer(whichtimer,seconds,microseconds)
ULONG seconds,microseconds;
struct IOStdReq *whichtimer;
{
struct Port *tempPort;
tempPort = whichtimer->io_Message.mn_ReplyPort;
whichtimer->io_Command = TR_ADDREQUEST; /* add a new timer request */
whichtimer->SECONDS = seconds; /* seconds */
whichtimer->MICROSECONDS = microseconds; /* microseconds */
SendIO(whichtimer);
return(tempPort);
}
int
WaitTimer(whichtimer,seconds,microseconds)
ULONG seconds,microseconds;
struct IOStdReq *whichtimer;
{
whichtimer->io_Command = TR_ADDREQUEST; /* add a new timer request */
whichtimer->SECONDS = seconds; /* seconds */
whichtimer->MICROSECONDS = microseconds; /* microseconds */
DoIO(whichtimer);
return(0);
}
int
DeleteTimer(whichtimer)
struct IOStdReq *whichtimer;
{
struct Port *whichport;
whichport = whichtimer->io_Message.mn_ReplyPort;
DeleteStdIO(whichtimer);
DeletePort(whichport);
return(0);
}
*************************************************************************
* HandlerInterface()
*
* This code is needed to convert the calling sequence performed by
* the input.task for the input stream management into something
* that a C program can understand.
*
* This routine expects a pointer to an InputEvent in A0, a pointer
* to a data area in A1. These values are transferred to the stack
* in the order that a C program would need to find them. Since the
* actual handler is written in C, this works out fine.
XREF _myhandler
XDEF _HandlerInterface
_HandlerInterface:
MOVEM.L A0/A1,-(A7)
JSR _myhandler
ADDQ.L #8,A7
RTS
END